﻿(function($) {
    $.fn.jtip = function(opt) {
        var options = $.extend({}, $.fn.jtip.defaults, opt || {});
        init(options);

        $(this).mouseover(function(e) {
            var $this = $(this);
            if ($this.is("a")
            && ($this.attr("target") == "tip" || $this.attr("target") == "imgtip")) {
                isshown = true;
                var url = unescape($this.attr("href")).replaceTmById($this.parents("form:first"));
                if (!url.isFinishedTm()) {
                    alertMsg.error($this.attr("warn"));
                    return false;
                }

                var cacheUrl = $this.data("url");
                if (cacheUrl == url) {
                    var tip = $this.data("tip");
                    if (tip) { showTip(e, tip, options.opacity); return; }
                }
                if ($this.attr("target") == "tip") {
                    $.ajax({
                        type: 'GET',
                        url: url,
                        data: null,
                        global: false,
                        cache: false,
                        success: function(response) {
                            var json = bee.jsonEval(response);
                            if (json.status != undefined) {
                                if (!json.status) {
                                    alertMsg.error(json.message);
                                }
                                else {
                                    showTip(e, json.result, options.opacity);
                                }
                            }
                            else {
                                $this.data("url", url);
                                $this.data("tip", response);
                                showTip(e, response, options.opacity);
                            }
                        },
                        error: bee.ajaxError
                    });
                }
                else {
                    var response = "<image src='" + url + "' height='200px'/>";
                    showTip(e, response, options.opacity);
                }
            }
            else {

                this.selftip = $this.attr("title");
                this.title = "";

                if (this.selftip != "")
                    showTip(e, this.selftip, options.opacity);
            }
        }).mouseout(function() {
            if (this.selftip) {
                this.title = this.selftip;
            }
            tipdiv.hide().children(":first").html("");
        }).mousemove(function(e) {
            isshown = true;
            showTip(e);
        });
    };
    $.fn.jtip.defaults = {
        maxwidth: 300,                              //最大宽度
        opacity: 0.8,                                  //透明度
        background: "#DEDFDD",                 //背景样式
        border: "#FEFFD4 solid 1px",           //边框样式
        contentcolor: "#000000",                 //正文色
        font: "12px verdana,arial,sans-serif" //字体样式
    };
    var tipdiv = null;
    var isshown = false;

    function showTip(evt, selftip, opacity) {
        if (selftip) {//仅用于over方法
            tipdiv.children(":first").html(selftip);

            tipdiv.css({
                "opacity": opacity
            }).show();
        }

        var top = evt.pageY + 20;
        var left = evt.pageX + 10;
        if ($(window).width() < left + tipdiv.width()) left -= tipdiv.width();
        if ($(window).height() < top + tipdiv.height()) top -= tipdiv.height();

        tipdiv.css({
            "top": (top) + "px",
            "left": (left) + "px"
        });
    }

    function init(options) {
        var tipobj = $("#jq_bbf_tipdiv");
        if (tipobj.length == 0) {
            tipobj = [];
            tipobj.push("<div id='jq_bbf_tipdiv' style='display:none;position:absolute;z-index:1001;max-width:");
            tipobj.push(options.maxwidth);
            tipobj.push("px;background:");
            tipobj.push(options.background);
            tipobj.push(";width:auto!important;width:auto;border:");
            tipobj.push(options.border);
            tipobj.push(";text-align:left;padding:3px;'><p style='margin:0;padding:0;color:");
            tipobj.push(options.contentcolor);
            tipobj.push(";font:");
            tipobj.push(options.font);
            tipobj.push(";'></p></div>");
            tipobj = $(tipobj.join("")).appendTo("body");

            $(window).mousemove(function(e) {
                if (!isshown) {
                    tipdiv.hide();
                }
                isshown = false;
            });
        }
        tipdiv = tipobj;
        tipobj = null;
    };
})(jQuery);